package com.example.templateapp.ui.widget

import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import com.example.templateapp.R
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshKernel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.SpinnerStyle
import com.scwang.smartrefresh.layout.internal.InternalAbstract

class FoxRefreshHeader : InternalAbstract, RefreshHeader {
    private lateinit var animationDrawable: AnimationDrawable
    private lateinit var contentView: View

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        contentView = LayoutInflater.from(context).inflate(R.layout.fox_refresh_header, this)
        val iv = view.findViewById<ImageView>(R.id.fox_loading_iv)
        animationDrawable = iv.background as AnimationDrawable
    }

    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Scale
    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
        super.onInitialized(kernel, height, maxDragHeight)
    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        animationDrawable.stop()
        super.onFinish(refreshLayout, success)
        return 500
    }

    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
        animationDrawable.start()
    }

    override fun getView(): View {
        return contentView
    }


    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
        animationDrawable.start()
    }


}